package cn.xny.system.service.impl;

import cn.xny.system.domain.Role;
import cn.xny.system.mapper.RoleMapper;
import cn.xny.system.service.RoleService;
import cn.xny.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {

     @Autowired
     private RoleMapper roleMapper;


    @Transactional
    @Override
    public void save(Role role) {
        super.save(role);
        //添加当前角色的菜单
        if(role.getMenus()!=null && role.getMenus().size() > 0){
            roleMapper.addRoleMenus(role.getId(),role.getMenus());
        }
        //添加当前角色的权限
        if (role.getPermissions()!=null && role.getPermissions().size()>0){
            roleMapper.addRolePermissions(role.getId(),role.getPermissions());

        }
    }

    @Transactional
    @Override
    public void del(Long id) {
        //删除当前角色的所有菜单
         roleMapper.delMenusByRoleId(id);
         //删除当前角色的所有权限
        roleMapper.delPermissionsByRoleId(id);
        //删除当前角色与员工的中间表数据
        roleMapper.delEmployeeByRoleId();
         //删除角色
        super.del(id);
    }

    @Transactional
    @Override
    public void update(Role role) {
        //删除当前角色的所有菜单
        roleMapper.delMenusByRoleId(role.getId());
        //删除当前角色的所有权限
        roleMapper.delPermissionsByRoleId(role.getId());

        //添加当前角色的菜单
        if(role.getMenus()!=null && role.getMenus().size()>0){
            roleMapper.addRoleMenus(role.getId(),role.getMenus());
        }
        //添加当前角色的权限
        if (role.getPermissions()!=null && role.getPermissions().size()>0){
            roleMapper.addRolePermissions(role.getId(),role.getPermissions());

        }
        super.update(role);
    }

    @Override
    public void patchDelete(List<Long> ids) {
      for (Long id: ids){
          del(id);
      }
    }
}
